package com.andkrup.web.ui {
	import com.andkrup.web.components.IComponentFactory;

	import flash.events.EventDispatcher;

	import com.andkrup.web.data.DataProvider;
	import com.andkrup.web.components.IComponent;
	import flash.display.DisplayObjectContainer;	
	
	/**
	 * @author Andkrup
	 */
	public class UiManager extends EventDispatcher implements IUiManager {
		protected var currComponent:IComponent;
		protected var _factory:IComponentFactory;

		public function get currentComponent():IComponent{
			return currComponent;
		}
		
		public function get factory():IComponentFactory {
			return _factory;
		}
		
		public function set factory(value:IComponentFactory):void {
			_factory = value;
		}
		
		public function UiManager(factory:IComponentFactory) {
			_factory = factory;
		}

		public function getComponent(id:*):IComponent{
			return _factory.createComponent(id);
		}
		
		public function displayComponent(base:DisplayObjectContainer, component:IComponent, dataProvider:DataProvider = null):void{
			if(currComponent){
				if(base.contains(currComponent.displayObject)){
					base.removeChild(currComponent.displayObject);
				}
				currComponent.kill();
				currComponent = undefined;
			}
			currComponent = component;
			if(currComponent){
				currComponent.init(dataProvider);
				base.addChild(currComponent.displayObject);
			}
		}
	}
}
